package com.cn.ycy.filter;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractNameValueGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.net.URI;

/**
 * GatewayFilterFactory
 */
@Component
public class AddTokenAccessFilterGatewayFilterFactory
        extends AbstractNameValueGatewayFilterFactory {

  private static Logger logger = LoggerFactory.getLogger(AddTokenAccessFilterGatewayFilterFactory.class);

  public String newValue = "2";

  @Override
  public GatewayFilter apply(NameValueConfig config) {
    return new GatewayFilter() {
      @Override
      public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //
        ServerWebExchange.Builder mutate = exchange.mutate();
        URI uri = exchange.getRequest().getURI();
        String rawQuery = uri.getRawQuery();
        MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams(); //用户带过的get参数
        ServerWebExchange build = mutate.build();
        logger.info(" add token name {} ,value {}", config.getName(), config.getValue());
        return chain.filter(exchange);
      }
    };
  }
}
